package com.everflourish.yeah100.http;

import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.everflourish.yeah100.entity.userinfo.UserInfo;
import com.everflourish.yeah100.utils.CommonUtil;
import com.everflourish.yeah100.utils.constant.RoleEnum;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * 设置请求接口封装
 *
 * @author pf
 * @date 2015-1-14
 */
public class SettingRequest extends BaseRequest {
    private static SettingRequest mRequest;

    private SettingRequest() {
    }

    public static SettingRequest getInstance() {
        if (mRequest == null) {
            mRequest = new SettingRequest();
        }
        return mRequest;
    }

    /**
     * 角色切换
     *
     * @param roleEnum
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest roleUpdateRequest(RoleEnum roleEnum,
                                               Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("role", roleEnum.name);
        JSONObject jsonObject = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_ROLE_UPDATE, jsonObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 退出程序求
     *
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest logoutRequest(Listener<JSONObject> listener,
                                           ErrorListener errorListener) {
        JSONObject jsonObject = new JSONObject();
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_SECURITY_LOGOUT, jsonObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 激活阅卷请求
     *
     * @param serialNumber  序列号
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest activateRequest(String serialNumber,
                                             Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("seriesNo", serialNumber);
        JSONObject jsoObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_SERIES_NO_BIND, jsoObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 使用条款同意
     *
     * @param map
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest activateUseTermsRequest(Map<String, Object> map,
                                                     Listener<JSONObject> listener, ErrorListener errorListener) {
        JSONObject jsoObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USE_TERMS_ACTIVATE, jsoObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 最新版本获取
     *
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest getLatestSoftwareVersionRequest(String versionClientName, Listener<JSONObject> listener,
                                                             ErrorListener errorListener) {
        try {
            versionClientName = URLEncoder.encode(versionClientName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String url = URL_PREFIX + "/software/version/latest/" + versionClientName;
        JsonObjectRequest request = new JsonObjectRequest(Method.GET, url,
                null, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 最新版本查询
     *
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest softwareVersionRequest(String versionClientName,
                                                    String versionCode, Listener<JSONObject> listener,
                                                    ErrorListener errorListener) {
        try {
            versionClientName = URLEncoder.encode(versionClientName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String url = URL_PREFIX + "/software/" + versionClientName
                + "/version/" + versionCode + "/detect";
        JsonObjectRequest request = new JsonObjectRequest(Method.GET, url,
                null, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 好友搜索
     *
     * @param condition     条件（姓名模糊查询）
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest searchRequest(String condition,
                                           Integer startIndex, Integer selectedCount,
                                           Listener<JSONObject> listener, ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("condition", condition);
            if (startIndex != null) {
                jsonObject.put("startIndex", startIndex);
            }
            if (selectedCount != null) {
                jsonObject.put("selectedCount", selectedCount);
            }
            request = new JsonObjectRequest(Method.POST, URL_USER_SEARCH,
                    jsonObject, listener, errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request;
    }

    /**
     * 修改密码请求
     *
     * @param oldPwd
     * @param newPwd
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest updatePwdRequest(String oldPwd, String newPwd,
                                              Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("oldPassword", CommonUtil.string2MD5(oldPwd));
        map.put("newPassword", CommonUtil.string2MD5(newPwd));
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_PASSWORD, jsonObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 用户手机号码更新
     *
     * @param phoneNo
     * @param phoneZone
     * @param verificationCode
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest userSettingUpdatePhoneRequest(String phoneNo, String phoneZone, String verificationCode,
                                                           Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phoneNo", phoneNo);
        map.put("phoneZone", phoneZone);
        map.put("verificationCode", verificationCode);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_PHONE, jsonObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 用户邮箱更新
     *
     * @param phoneNo
     * @param phoneZone
     * @param verificationCode
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest userSettingUpdateEmailRequest(String phoneNo, String phoneZone, String verificationCode,
                                                           String email,Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phoneNo", phoneNo);
        map.put("phoneZone", phoneZone);
        map.put("verificationCode", verificationCode);
        map.put("email", email);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_EMAIL, jsonObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 删除好友请求
     *
     * @param friendId      好友的id
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest deleteFriendRequest(String friendId,
                                                 Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("friendId", friendId);
        JSONObject jsoObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_FRIEND_DELETE, jsoObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 删除好友请求
     *
     * @param friendId      好友的id
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest FriendNickNameUpdateRequest(String friendId,
                                                         String nickName, Listener<JSONObject> listener,
                                                         ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("friendId", friendId);
        if (nickName != null) {
            map.put("nickName", nickName);
        }
        JSONObject jsoObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_FRIEND_NICKNAME, jsoObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 接受好友邀请请求
     *
     * @param friendId      好友的id
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest acceptFriendRequest(String friendId,
                                                 Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("friendId", friendId);
        JSONObject jsoObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_FRIEND_ACCEPT, jsoObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 添加好友邀请请求
     *
     * @param friendId      好友的id
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest addFriendRequest(String friendId,
                                              Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("friendId", friendId);
        JSONObject jsoObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_FRIEND_INVITE, jsoObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 获取好友列表请求
     *
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest friendListRequest(Integer startIndex,
                                               Integer selectedCount, Listener<JSONObject> listener,
                                               ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            JSONObject object = new JSONObject();
            if (startIndex != null) {
                object.put("startIndex", startIndex + "");
            }
            if (selectedCount != null) {
                object.put("selectedCount", selectedCount + "");
            }
            request = new JsonObjectRequest(Method.POST, URL_USER_FRIEND,
                    object, listener, errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request;
    }

    /**
     * 用户反馈请求
     *
     * @param userFeedback  反馈内容
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest feedbackRequest(String userFeedback,
                                             Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("text", userFeedback);
        JSONObject jsoObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_feedback, jsoObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 用户反馈请求
     *
     * @param startIndex
     * @param selectedCount
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest guidelineListRequest(Integer startIndex,
                                                  Integer selectedCount, Listener<JSONObject> listener,
                                                  ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            JSONObject jsonObject = new JSONObject();
            if (startIndex != null) {
                jsonObject.put("startIndex", startIndex);
            }
            if (selectedCount != null) {
                jsonObject.put("selectedCount", selectedCount);
            }
            request = new JsonObjectRequest(Method.POST, URL_GUIDELINE_LIST,
                    jsonObject, listener, errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request;
    }

    /**
     * 用户信息更新
     *
     * @param userInfo      用户信息实体类
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest userInfoUpdateRequest(UserInfo userInfo,
                                                   Listener<JSONObject> listener, ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            JSONObject userInfoJson = new JSONObject();
            if (!TextUtils.isEmpty(userInfo.id)) {
                userInfoJson.put("id", userInfo.id);
            }
            if (!TextUtils.isEmpty(userInfo.name)) {
                userInfoJson.put("name", userInfo.name);
            }
            if (!TextUtils.isEmpty(userInfo.nickName)) {
                userInfoJson.put("nickName", userInfo.nickName);
            }
            if (!TextUtils.isEmpty(userInfo.motto)) {
                userInfoJson.put("motto", userInfo.motto);
            }
            if (!TextUtils.isEmpty(userInfo.photoId)) {
                userInfoJson.put("imageId", userInfo.photoId);
            }
            if (!TextUtils.isEmpty(userInfo.email)) {
                userInfoJson.put("email", userInfo.email);
            }
            if (!TextUtils.isEmpty(userInfo.phoneNo)) {
                userInfoJson.put("phoneNo", userInfo.phoneNo);
                userInfoJson.put("phoneZone", "86");
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userInfo", userInfoJson);
            request = new JsonObjectRequest(Method.POST, URL_USER_UPDATE,
                    jsonObject, listener, errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request;
    }
}
